﻿using System;
using System.Collections.Generic;
using System.Text;
using HR.Appointments.DataLayer;
using HR.Appointments.Service.ValueObjects;

namespace HR.Appointments.Service
{
    /// <summary>
    /// Entity class for companies
    /// </summary>
    public class CompanyEntity
    {
        #region Member variable
        private int _companyId; 
        private string  _nationalId;
        private string _description;
        private string _name;
        private string _phone;
        private string _email;
        private int _delayTime;
        private TimeDuration _slotDuration;
        private string _picture;
        private string _username;
        private string _password;
        #endregion

        #region Constructors
        private CompanyEntity(int companyId, string nationalId, string description, string name,
            string phone, string email, int delayTime, string picture, string username, string password)
        {
            _companyId = companyId;
            _nationalId = nationalId;
            _description = description;
            _name = name;
            _phone = phone;
            _email = email;
            _delayTime = delayTime;
            _slotDuration = TimeDuration.Create(30);
            _picture = picture;
            _username = username;
            _password = password;
        }
        #endregion

        #region Properties
        /// <summary>
        /// National identifier for the company
        /// </summary>
        public string NationalId
        {
            get
            {
                return _nationalId;
            }
            set
            {
                _nationalId = value; 
            }
        }

        /// <summary>
        /// Company description
        /// </summary>
        public string Description
        {
            get
            {
                return _description; 
            }
            set
            {
                _description = value; 
            }
        }

        /// <summary>
        /// Company Name
        /// </summary>
        public string Name
        {
            get
            {
                return _name; 
            }
            set
            {
                _name = value; 
            }
        }

        /// <summary>
        /// Phone number of the company
        /// </summary>
        public string Phone
        {
            get
            {
                return _phone; 
            }
            set
            {
                _phone = value; 
            }
        }

        /// <summary>
        /// Email for the company
        /// </summary>
        public string Email
        {
            get
            {
                return _email; 
            }
            set
            {
                _email = value; 
            }
        }

        /// <summary>
        /// Number of days from when customer creates an order until the
        /// company recieves an notification that the order has not been
        /// confirmed
        /// </summary>
        public int DelayTime
        {
            get
            {
                return _delayTime; 
            }
            set
            {
                _delayTime = value; 
            }
        }

        /// <summary>
        /// Company identifier
        /// </summary>
        public int CompanyId
        {
            get
            {
                return _companyId; 
            }
            set
            {
                _companyId = value; 
            }
        }

        /// <summary>
        /// Username for the company.  This should always be unique.
        /// </summary>
        public string Username
        {
            get
            {
                return _username;
            }
            set
            {
                _username = value;
            }
        }

        /// <summary>
        /// Password to match the username
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
            }
        }

        /// <summary>
        /// Gets and sets the slotsduration in minutes.
        /// Default value is 30 min.
        /// </summary>
        public int SlotDuration
        {
            get
            {
                if (_slotDuration != null)
                    return _slotDuration.DurationValue;
                else
                    return _slotDuration.Default;
            }
        }

        /// <summary>
        /// Get the picture URL
        /// </summary>
        public string Picture
        {
            get
            {
                if(string.IsNullOrEmpty(_picture.Trim()))
                {
                    //TODO:  setja sem app config.
                    return "~/Myndir/rakari3.jpg";
                }
                else
                {
                    return _picture;
                }
            }
            set
            {
                _picture = value;
            }
        }
        #endregion 

        #region Internal Methods
        /// <summary>
        /// Convert this object to datalayer 
        /// </summary>
        /// <param name="data">Datalayer entity</param>
        /// <returns>A datalayer entity with this objects values</returns>
        internal Company ToData(Company data)
        {
			if (data == null)
			{
				data = new Company();
			}
            data.CompanyId = _companyId;
            data.DelayTime = _delayTime;
            data.Description = _description;
            data.Email = _email;
            data.Name = _name;
            data.NationalId = _nationalId;
            data.Phone = _phone;
            data.Picture = _picture;
            data.Username = _username;
            data.Password = _password;
            data.DelayTime = _delayTime;
            return data;
        }
        #endregion

        #region Override Methods
        /// <summary>
        /// Returns a String that represents the current Object.
        /// </summary>
        /// <returns>A String that represents the current Object.</returns>
        public override string ToString()
        {
            return _nationalId + " " + _name + " " + _email;
        }
        #endregion

        #region Static Methods
        /// <summary>
        /// Loads a datalayer entity into a service entity.
        /// </summary>
        /// <param name="row"></param>
        /// <returns>A loaded entity</returns>
        internal static CompanyEntity Load(HR.Appointments.DataLayer.Company row)
        {
            if (row == null) return null;
            return new CompanyEntity(row.CompanyId, row.NationalId, row.Description, row.Name,
                row.Phone, row.Email, row.DelayTime, row.Picture, row.Username, row.Password);
        }

        /// <summary>
        /// Creates a new entity
        /// </summary>
        /// <param name="nationalId">Id for the company given by the country</param>
        /// <param name="description">A short description of the company</param>
        /// <param name="name">The company name</param>
        /// <param name="phone">A valid phone number</param>
        /// <param name="email">A valid email address</param>
        /// <param name="picture">Server url to the picture</param>
        /// <param name="username">Username for the company</param>
        /// <param name="password">Password for the user</param>
        /// <param name="delaytime">Number of days from creation of n order until the company recieves 
        /// an notification that the order has not been confirmed</param>
        /// <returns>A new company entity</returns>
        public static CompanyEntity Create(string nationalId, string description, 
            string name, string phone, string email, string picture, string username, string password, int delaytime)
        {
            return new CompanyEntity(0, nationalId, description, name, phone, email,
                delaytime, picture, username, password);
        }
        #endregion
    }
}
